#####################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
#####################################################
# Figure 1 and Table 1 (Baseline support)
#####################################################

# define which packages are needed
p_needed <-
  c("broom", "hrbrthemes", "viridis", "ggh4x", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages
sapply(p_needed, require, character.only = TRUE)

#####################################################

# load data
load("EUP_Costs_ReplicationData.RData")

# Experiment 1: trichotomize data
dat_desc_military <- dat %>%
  filter(costExperiment1_Treat1 == "Control" & costExperiment1_Treat2 == "Control") %>%
  drop_na(costExperiment1_CrisisManagement) %>%
  select(QCOUNTRY, costExperiment1_CrisisManagement) %>%
  mutate(dv = "Military Intervention") %>%
  rename(scale = costExperiment1_CrisisManagement) %>%
  mutate(support_tri = case_when(scale > 0.5 ~  "Support",
                                 scale == 0.5 ~  "Neither Support nor Oppose",
                                 scale < 0.5 ~  "Oppose"))

# calculate share for each category
military_baseline <- dat_desc_military %>%
  group_by(support_tri) %>%
  summarise(n = n()) %>%
  mutate(freq = n / sum(n),
         exp = "Military Intervention")

# Experiment 1: trichotomize data
dat_desc_aircraft <- dat %>%
  filter(costExperiment2_Treat1 == "Control" & costExperiment2_Treat2 == "Control") %>%
  drop_na(costExperiment2_Aircraft) %>%
  select(QCOUNTRY, costExperiment2_Aircraft) %>%
  mutate(dv = "Defence Procurement") %>%
  rename(scale = costExperiment2_Aircraft) %>%
  mutate(support_tri = case_when(scale > 0.5 ~  "Support",
                                 scale == 0.5 ~  "Neither Support nor Oppose",
                                 scale < 0.5 ~  "Oppose"))

# calculate share for each category
aircraft_baseline <- dat_desc_aircraft %>%
  group_by(support_tri) %>%
  summarise(n = n()) %>%
  mutate(freq = n / sum(n),
         exp = "Defence Procurement")

# bind data from both experiments for visualization
dat_desc <- rbind(dat_desc_military,
                  dat_desc_aircraft)

# Table 1: 
table1 <- rbind(aircraft_baseline, military_baseline)

table1

# Figure 1:
baseline <- dat_desc %>%
  ggplot(.) +
  geom_bar(aes(x = fct_rev(QCOUNTRY), fill = factor(support_tri, levels=c("Support","Neither Support nor Oppose","Oppose"))), position = 'fill') +
  ylab('Proportion of Support') +
  xlab("") +
  ggtitle('')+
  facet_wrap(~ factor(dv, levels = c("Military Intervention", "Defence Procurement"))) +
  coord_flip() +
  scale_y_reverse(label=c("1","0.75","0.5", "0.25", "0")) +
  theme_ipsum_rc(grid="",
                 axis_title_size = 20,
                 axis_text_size = 24,
                 strip_text_size = 28,
                 strip_text_face = "bold") +
  #scale_fill_discrete(labels = c("Strongly Oppose", "Somewhat Oppose", "Neither Support nor Oppose", "Somewhat Support", "Strongly Support")) +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        legend.text = element_text(size = 24),
        strip.text.x = element_text(hjust = 0.5),
        panel.spacing.x = unit(0, "lines")) +
  scale_fill_grey()

# save plot 
ggsave("plots/descriptives_Experiments.png", baseline, device = "jpeg", width = 4200 , height = 4000, units = "px")
